package loadiconhandler

import (
	"fmt"
	"gmap-backend/internal/config"
	"gmap-backend/internal/server/vo"
	"gmap-backend/internal/util/pathutil"
	"strings"
)

type DirLoadIcon struct {
}

func (owner *DirLoadIcon) CanHandle(appConfig *config.AppConfig, req *vo.PathReq) (bool, any) {
	urlMatch := strings.HasPrefix(req.Path, "dir:///") || strings.HasPrefix(req.Path, "dir://")
	if urlMatch {
		path := strings.TrimSpace(strings.TrimPrefix(strings.TrimPrefix(req.Path, "dir:///"), "dir://"))
		return true, path
	}
	return false, nil
}

// 当为目录时，结果为目录和对勾的叠加
// 当为文件时，结果为文件对应的图标与对勾的叠加
func (owner *DirLoadIcon) Handle(appConfig *config.AppConfig, req *vo.PathReq, extra any) *vo.LoadIconResp {
	path := extra.(string)
	parseResult := pathutil.ParseValidPath(path, req.CtxDir)
	if !parseResult.Succ {
		panic(fmt.Sprintf("无法匹配到有效路径：\r\n%s", path))
	}
	if parseResult.Dir {
		return &vo.LoadIconResp{
			Cascade: true,
			Url:     SPECIAL_PATH_FOLDER,
			Url2:    SPECIAL_PATH_DIR,
		}
	}
	return &vo.LoadIconResp{
		Cascade: true,
		Url:     GetFileIconUrl(appConfig, parseResult.ValidPath).Url,
		Url2:    SPECIAL_PATH_DIR,
	}
}
